# encoding: utf-8
from openai import OpenAI

import pandas as pd
from conf import QwenModel,DS,Doubao,key2fastgpt
from base import *
# messages  =[  {"role": "system", "content": ''},
#            # {"role": "assistant", "content": start},
#             {"role": "user", "content": prompt}
#         ]
#
#print(chat_fastgpt("https://cloud.fastgpt.cn/api/v1",apikey,'你是谁'))
qwen_client = get_client(QwenModel)
doubao_client = get_client(Doubao)
ds_client = get_client(DS)
def chat_fastgpt(base_url,apikey,q,variables={}):
    """

        "variables": {
        "uid": "asdfadsfasfd2323",
        "name": "张三"
    }
    :param base_url:
    :param apikey:
    :param q:
    :return:
    """
    client = OpenAI(api_key=apikey, base_url=base_url)
    ans = ''
    response = client.chat.completions.create(
        model='t',
        messages=[
            {'role': 'user', 'content': q}
        ],
        extra_body={'variables': variables},
        stream=True
    )
    for chunk in response:
        if chunk.choices:
            #print(chunk.choices[0].delta.content, end='')
            if  chunk.choices[0].delta.content:
                ans += chunk.choices[0].delta.content
    return ans



def perform_ai_reply(qs,key2fastgpt):
    rows = []
    for q in qs:
        print('current',q)
        d = {'q':q}
        variables = {
            'user_labels': "{'职业类型'：['自由职业/创业者']},'学历背景':['硕士'],'毕业年限':['5-10年'],'考研目的':[‘职称’],'考研跳转':['经济压力']}"}
        d['人设'] = variables['user_labels']
        for name,apikey in key2fastgpt.items():
            #fastgpt-hwMMuuHlSWzJt6dEdvplVd2d2lJNXyNxYwI4UHlHB23jHzQjMyd45ixDh6fargP
            res = chat_fastgpt("http://47.107.66.213:3000/api/v1", apikey, q,variables)
            print(res)
            d[name] = res
        rows.append(d)
    # for sys in [system_prompt3,system_prompt6]:
    #     for q in qs:
    #         messages[-1]['content'] = q
    #         messages[0]['content'] = sys
    #         a1 = qwen_chat(qwen_client,QwenModel.model,messages)
    #         a2 = qwen_chat(ds_client,DS.model,messages)
    #         a3 = qwen_chat(doubao_client,Doubao.model,messages)
    #         rows.append({'系统提示词':sys,'q':q,'qwen':a1,'ds':a2,'doubao':a3})
    # #
    df = pd.DataFrame(rows)
    df.to_excel('新提示词0522_12am.xlsx')
# 0522 新v4提示词
#fastgpt-wwHiOA2ogamhulCK5ew8jphMBws9ZmvGWUJiDRWjStJ6aV7sJv12RN13Mbo
#9ebe3329-752d-41af-8c78-5cc66ffd5d20
if __name__ == '__main__':
    qs = ['你了解我么'] + list(pd.read_excel('副本测试的问题.xlsx')['问题'].values)
    perform_ai_reply(qs,key2fastgpt)# 然后运行run_eval.py